package com.hailong.zyt.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hailong.zyt.beans.Teacher;
import com.hailong.zyt.service.TeacherService;
import com.hailong.zyt.service.impl.TeacherServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hailong
 * @since 2021-05-13
 */
@Slf4j
@Controller
public class TeacherController {

    private TeacherService teacherService;

    @Autowired
    public void setTeacherService(TeacherServiceImpl teacherService) {
        this.teacherService = teacherService;
    }

    /*-----------------------------------------------管理员------------------------------------------------------------*/
    @GetMapping("/admin/teachers")
    public String toTeacher(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model) {
        Page<Teacher> teacherPage = new Page<>(pn, 8);
        IPage<Teacher> page = teacherService.page(teacherPage);
        model.addAttribute("teachers", page);
        log.info("pages:{}", page.getPages());
        return "/admin/admTeacher";
    }

    @GetMapping("/admin/teacher/{teaId}")
    public String toEditTeacher(@PathVariable("teaId") Long teaId, Model model) {
        Teacher teacher = teacherService.getById(teaId);
        model.addAttribute("teacher", teacher);
        return "/admin/editTeacher";
    }

    //修改教师信息
    @PutMapping("/admin/teacher")
    public String editTeacher(Teacher teacher) {
        teacherService.updateById(teacher);
        return "redirect:/admin/teachers";
    }
    //添加教师账号
    @PostMapping("/admin/addTeacher")
    public String addTeacher(Teacher newTeacher, Model model) {
        boolean saved = teacherService.addTeacher(newTeacher);
        model.addAttribute("add_msg", "添加成功");
        return "redirect:/admin/teachers";
    }

    //删除教师账号
    @DeleteMapping("/admin/deleteTeacher/{teaId}")
    public String deleteTeacher(@PathVariable("teaId") Long teaId) {
        teacherService.removeById(teaId);
        return "redirect:/admin/teachers";
    }
    /*----------------------------------------------------------------------------------------------------------------*/



    /*--------------------------------------------------教师-----------------------------------------------------------*/

    /*
     * 教师登录
     * */
    @GetMapping("/index/teaLogin")
    public String toTeaLogin(){
        return "/index/teacherLogin";
    }

    @PostMapping("/index/teaLogin")
    public String teaLogin(Teacher user, HttpSession session) {
        Teacher teacher = teacherService.login(user);
        if(teacher == null) {
            return "/index/teacherLogin";
        }
        else {
            session.setAttribute("teacher", teacher);
            session.setAttribute("userType", "teacher");
            return "redirect:/teacher/main";
        }
    }

    /**
     * 退出登录
     * */
    @GetMapping("/teacher/loginOut")
    public String loginOut(HttpSession session) {
        session.removeAttribute("teacher");
        return "redirect:/";
    }

    /**
    * 首页
    * */
    @GetMapping("/teacher/main")
    public String toMain(HttpSession session, Model model){
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        model.addAttribute("teacher", teacher);
        model.addAttribute("courseCount", teacherService.getCourseCount(teacher.getTeaId()));
        model.addAttribute("studentCount", teacherService.getStudentCount(teacher.getTeaId()));
        model.addAttribute("homeworkRate", teacherService.getHWFinishRate(teacher.getTeaId()));
        return "/teacher/teaMain";
    }


    @GetMapping("/teacher/editCourse")
    public String toEdit(){
        return "/teacher/course/editCourse";
    }


    /**
     * 修改密码
     * */
    @GetMapping("/teacher/info")
    public String toInfo(HttpSession session, Model model){
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        model.addAttribute("teacher", teacher);
        return "/teacher/teaInfo";
    }

    @PutMapping("/teacher/updateInfo")
    public String updateInfo(Teacher teacher, HttpSession session){
        teacherService.saveOrUpdate(teacher);
        session.setAttribute("teacher", teacher);
        return "redirect:/teacher/info";
    }

    @PutMapping("/teacher/changePassword")
    public String changePassword(Teacher newTeacher, String newPassword, String reNewPassword, HttpSession session, Model model){
        Teacher teacher = teacherService.getById(newTeacher.getTeaId());
        if(!teacher.getTeaPassword().equals(newTeacher.getTeaPassword())){
            model.addAttribute("msg", "旧密码不正确");
            return "/teacher/teaInfo";
        }
        else{
            teacher.setTeaPassword(newPassword);
            teacherService.saveOrUpdate(teacher);
            session.setAttribute("teacher", teacher);
            return "redirect:/teacher/info";
        }
    }
    /*----------------------------------------------------------------------------------------------------------------*/
}

